package com.lkd.controller;

import com.lkd.utils.AliOssUtil;
import com.lkd.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {
    @Autowired
    private AliOssUtil aliOssUtil;
    @PostMapping("/api/vm-service/sku/fileUpload")
    public Result uploadFile(MultipartFile fileName) throws IOException {
        // 校验是否为图片文件
        if (fileName.getSize() == 0) {
            throw new RuntimeException("上传图片不能为空");
        }

        // 获得原始文件名
        String originalFilename = fileName.getOriginalFilename();

        // 获得文件扩展名
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName1 = UUID.randomUUID().toString() + extension;

        String filePath = aliOssUtil.upload(fileName1,fileName.getInputStream());
        return Result.success(filePath);
    }
}
